<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <el-form ref="queryForm" :model="queryParams" label-width="89px" v-show="showSearch" class="ele-form-search"
             @keyup.enter.native="reload"
             @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :lg="8" :md="12">
          <el-form-item label="产品名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<!--            <el-col :span="1.5">-->
<!--              <el-button-->
<!--                type="warning"-->
<!--                plain-->
<!--                icon="el-icon-download"-->
<!--                size="mini"-->
<!--                @click="handleExport"-->
<!--                v-hasPermi="['qualityManagement:inboundInspection:export']"-->
<!--              >导出-->
<!--              </el-button>-->
<!--            </el-col>-->
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">

        </el-col>
      </el-row>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->

    <!--     -->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="inboundInspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="入库检记录ID" align="center" prop="inspectionId" />-->
      <el-table-column label="产品名称" align="center" prop="materialName" width="200" show-overflow-tooltip/>
      <!--      <el-table-column label="仓库名称" align="center" prop="warehouseName" />-->
      <!--      <el-table-column label="检验项目" align="center" prop="inspectionItem" />-->
      <el-table-column label="质检结果" align="center" prop="zhijian">
        <template slot-scope="scope">
          <span>{{ scope.row.zhijian == 1 ? "待检" : scope.row.zhijian == 2 ? "合格" : '不合格' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="出库类型" align="center" prop="libraryType">
        <template slot-scope="scope">
          <span>{{
              scope.row.shippingType == 4 ? "销售出库" : scope.row.shippingType == 1 ? "领料出库" : scope.row.shippingType == 2 ? '外采出库' : scope.row.shippingType == 3 ? '退货出库' : " "
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库数量" align="center" prop="shipmentQuantity"/>
      <!--      <el-table-column label="检验日期" align="center" prop="cjdata" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.cjdata, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="检验员" align="center" prop="createUser"/>
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="formm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="质检结果:" prop="zhijian">
          <!--          <el-select clearable v-model="form.zhijian" placeholder="请选择质检结果" class="ele-fluid">-->
          <!--            <el-option v-for="item in inspectionResultList" :key="item.type" :label="item.name" :value="item.type" />-->
          <!--          </el-select>-->
          <el-radio-group v-model="form.zhijian" class="ele-fluid">
            <el-radio v-for="item in inspectionResultList" :key="item.type" :label="item.type">{{
                item.name
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="remark" v-if="form.zhijian === 3" label="不合格原因:">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入不合格原因"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInboundInspection,
  getInboundInspection,
  delInboundInspection,
  addInboundInspection,
  updateInboundInspection
} from "@/api/qualityManagement/inboundInspection";
import {delProductionEceipt} from "@/api/InboundManagement/productionEceipt";
import {getUserProfile} from "@/api/system/user";

export default {
  name: "InboundInspection",
  data() {
    return {
      inspectionId: '',
      xiugaiForm: {
        zhijian: '',
        //产品id
        deliveryId: '',
        // inspectionId
      },
      user: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库检管理表格数据
      inboundInspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        materialName: null,
        warehouseName: null,
        inspectionItem: null,
        inspectionResult: null,
        inspectionDate: null,
        inspector: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      // 质检结果
      inspectionResultList: [
        // {
        //   type:1,name:"待检"
        // },
        {
          type: 2, name: "合格"
        },
        {
          type: 3, name: "不合格"
        }
      ],

      // 表单校验
      rules: {
        zhijian: [{required: true, message: '请选择质检结果', trigger: 'change'}],
        remark: [
          {required: true, message: "请填写不合格原因", trigger: "blur"}
        ],
      },
    };
  },
  // created() {
  //   this.getList();
  // },
  mounted() {
    this.getList()
    this.getUser()
  },
  methods: {
    /** 查询入库检管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.zhijian = this.queryParams.zhijians || '1,2,3'
      this.queryParams.storageStatus = 2
      // this.queryParams.zhijian = 1,3
      listInboundInspection(this.queryParams).then(response => {
        //console.log("ppppp", response)
        this.inboundInspectionList = response.rows;
        // this.inboundInspectionList.forEach((item) => {
        //   item.libraryType = Number(item.libraryType);
        // });
        this.total = response.total;
        // //console.log("ppppp",  this.total)
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inspectionId: null,
        materialName: null,
        warehouseName: null,
        inspectionItem: null,
        inspectionResult: null,
        inspectionDate: null,
        inspector: null,
        remarks: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deliveryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加入库检管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.deliveryId = row.deliveryId || this.ids
      getInboundInspection(this.deliveryId).then(response => {
        //console.log('row', row)
        this.form = {...row};
        this.form.zhijian = Number(row.zhijian);
        this.open = true;
        this.title = "修改入库检管理";
      });
    },
    validateReason(rule, value, callback) {
      if (this.form.zhijian === 3 && (!value || value.trim() === '')) {
        callback(new Error('请输入不合格原因'));
      } else {
        callback();
      }
    },
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        //console.log(this.user)
      });
    },

    /** 提交按钮 */
    submitForm() {
      //console.log('提交表单');
      this.$refs.formm.validate(valid => {
        if (valid) {
          if (this.ids.length === 0) {
            //console.log('执行修改操作', this.form);
            this.xiugaiForm.createUser = this.user.createBy;
            this.xiugaiForm.zhijian = this.form.zhijian;
            this.xiugaiForm.deliveryId = this.deliveryId;
            this.xiugaiForm.remark = this.form.remark;
            this.xiugaiForm.materialId = this.form.materialId;
            this.xiugaiForm.totalAmount = this.form.totalAmount;
            updateInboundInspection(this.xiugaiForm).then(response => {
              //console.log('修改成功', response);
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            }).catch(error => {
              //console.log('修改失败', error);
            });
          } else {
            //console.log('执行新增操作', this.form);
            addInboundInspection(this.form).then(response => {
              //console.log('新增成功', response);
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            }).catch(error => {
              //console.log('新增失败', error);
            });
          }
        } else {
          //console.log('表单验证未通过');
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const inspectionIds = row.deliveryId || this.ids;
      this.$modal.confirm('是否确认删除入库检管理编号为"' + inspectionIds + '"的数据项？').then(function () {
        return delProductionEceipt(inspectionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qualityManagement/inboundInspection/export', {
        ...this.queryParams
      }, `inboundInspection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
